<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            position: absolute;
            left: 0;
            top: 0;
            background: palevioletred;
        }
    </style>
</head>
<body>
    <div id="box">box</div>
    <script>
        function Drag (selector) {
            this.box = document.querySelector(selector)
            this.bindEvent()
        }
        Object.assign(Drag.prototype,{
            bindEvent () {
                this.box.onmousedown = (e) => {
                    let disx = e.offsetX,
                        disy = e.offsetY
                    document.onmousemove = (e) => {
                        let left = e.clientX - disx,
                            top = e.clientY - disy
                        this.move(left,top)    
                    }
                    document.onmouseup = function (){
                        this.onmousemove = null
                    }
                    return false
                }
            },
            move (left,top) {
                this.box.style.left = left + 'px'
                this.box.style.top = top + 'px'
            }
        })
        new Drag('#box')
    </script>
</body>
</html>